package servlets;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class OtherServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //从request域中读取user属性
        String user = (String) req.getAttribute("user");

        //获取Session
        HttpSession session = req.getSession(false);

        //使Session失效
//        session.invalidate();
//        System.out.println("session = "+session);

        //从Session中读取指定属性
        String username = null;
        if (session != null) {
            username = (String) session.getAttribute("username");
        }

        resp.setContentType("text/html;charset=utf-8");

        PrintWriter writer = resp.getWriter();
        writer.println("OtherServlet: user = "+user);
        writer.println("OtherServlet: username = "+username);
        writer.println("OtherServlet: session = "+session);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doGet(req,resp);
    }
}
